package org.example.hotel.entity;

import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import lombok.Data;
import lombok.Getter;

import java.time.Duration;
import java.time.LocalDateTime;

@Entity
@Data
public class Room {

    @Id
    private Integer roomId;

    private boolean occupied;
    private Double initialTemp = 25.0;
    private Double currentTemp = 25.0;
    private Double targetTemp = 25.0;

    private String fanSpeed = "MEDIUM"; // 风速：LOW / MEDIUM / HIGH
    private boolean serving = false;    // 是否正在送风
    private boolean acOn = false;       // 是否开机
    private String workMode = "COLD";   // 工作模式：COLD / HOT

    private LocalDateTime checkInTime;
    private LocalDateTime checkOutTime;

    private LocalDateTime lastSpeedChangeTime;

    // 调度相关状态
    private LocalDateTime serviceStartTime;  // 开始送风时间
    private LocalDateTime waitStartTime;     // 开始等待时间

    // ========== 方法 ==========

    public long getServiceDuration() {
        return (serviceStartTime != null)
                ? Duration.between(serviceStartTime, LocalDateTime.now()).toMillis()
                : 0L;
    }

    public Long getWaitDuration() {
        return (waitStartTime != null)
                ? Duration.between(waitStartTime, LocalDateTime.now()).toMillis()
                : null;
    }

    public void refreshServiceTime() {
        this.serviceStartTime = LocalDateTime.now();
    }

    public void startWaiting() {
        this.waitStartTime = LocalDateTime.now();
    }


    public void clearWaitStartTime() {
        this.waitStartTime = null;
    }

    public boolean isDebounced() {
        return lastSpeedChangeTime != null &&
                Duration.between(lastSpeedChangeTime, LocalDateTime.now()).toMillis() < 1000;
    }

    public void recordSpeedChangeTime() {
        this.lastSpeedChangeTime = LocalDateTime.now();
    }

    public WindSpeed getWindSpeedEnum() {
        return WindSpeed.valueOf(fanSpeed.toUpperCase());
    }

    public void clearServiceState() {
        this.setServing(false);
        this.setWaitStartTime(null);
        this.setServiceStartTime(null);
    }

    @Getter
    public enum WindSpeed {
        HIGH(3), MEDIUM(2), LOW(1);
        private final int priority;
        WindSpeed(int priority) {
            this.priority = priority;
        }
    }
}
